package hu.issue.framework.web.validation;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class ErrorMessages {
	private final Map<String, List<String>> fieldMessages = new HashMap<String, List<String>>();
	private final List<String> messages = new LinkedList<String>();

	public void addFieldMessage(final String field, final String message) {
		getField(field).add(message);
	}

	public String getFieldMessage(final String field) {
		final List<String> messages = getField(field);
		if (messages.isEmpty()) {
			return null;
		}

		return messages.get(0);
	}

	private List<String> getField(final String field) {
		if (!this.fieldMessages.containsKey(field)) {
			this.fieldMessages.put(field, new LinkedList<String>());
		}

		return this.fieldMessages.get(field);
	}

	public List<String> getErrorMessages() {
		return Collections.unmodifiableList(this.messages);
	}

	public void addMessage(final String message) {
		this.messages.add(message);
	}
}
